# -*- coding: iso-8859-1 -*-
from time import sleep
import audiere
import sys, os

# import mp3GUI
from gui_functions import *

from mp3 import *

getTags = True

if getTags:
    from ID3 import *

class AudioPlayer:
    cur_song = "<NONE>"
    def __init__(self, playlistfile="playlist.txt"):
        # Open default audio device
        self.dev = audiere.open_device()

        # Load playlist
        #playlistf = open(playlistfile, 'r')
        #self.playlist = [song for song in playlistf.read().split('\n')]
        #playlistf.close()
        self.songStartingTime = 0
        self.songDuration = 0

    def playFile(self, track):
        if os.path.isfile(track):
            f = self.dev.open_file(track,1)
            try:
                mp3inf = mp3info(track)
                self.songDuration = mp3inf['LENGTH']
            except:
                self.songDuration = 0
            
            f.volume = 1
            
            if getTags:
                try:
                    id3info = ID3(track)
                    try:
                        tr_artist = id3info['ARTIST']
                    except KeyError:
                        tr_artist = "<UNKNOWN>"
                        
                    try:
                        tr_title = id3info['TITLE']
                    except KeyError:
                        tr_title = "<UNKNOWN>"

                    try:
                        tr_year = id3info['YEAR']
                    except KeyError:
                        tr_year = "<UNKNOWN>"

                    if not (tr_year == "<UNKNOWN>" or tr_year == ""):
                        tr_year = " ("+tr_year+")"
                    else:
                        tr_year = ""
                        
                    if tr_artist == "<UNKNOWN>" or tr_artist == "":
                        tr_artist = "Unknown Artist"
                    if tr_title == "<UNKNOWN>" or tr_title == "":
                        tr_title = "Unknown Artist"
                    
                    print("\nNow playing: %s ~%s %s" % (tr_artist, tr_year, tr_title))
                    print("\nFile: %s" % track)
                except InvalidTagError, message:
                    print("\nInvalid ID3 tag: %s" % message)
            else:
                print("\nNow playing: %s" % track)
            
            f.play()
            self.songStartingTime = time.time()

            self.cur_song = f
        else:
            self.cur_song = "<NONE>"
            print("Unknown track '%s'! (skipped)\n" % track)

test = AudioPlayer()

window = mp3GUI(None,-1,'py-audio-gui ~ mp3 player',test)
window.start()

#u_input = raw_input("What to do? (play/exit): ")
#if u_input=="play" or u_input=="p":
#    try:
#        test.play()
#    except KeyboardInterrupt:
#        test.cur_song.stop()
#        print("\n\nProgram aborted.")
#elif u_input=="exit" or u_input=="quit" or u_input=="q":
#    print("Program aborted.")
#    exit
